package com.toutiao.issue


import scala.collection.mutable.{HashMap => HMap}

import scala.io.Source
import scala.collection.mutable.ArrayBuffer

import java.util.Date

class UpperDict {
  
  private
  val mData = new HMap[String,Float]
  
  def apply(k:String) = mData(k)
  
  def size = mData.size
  def keySet = mData.keySet
  
  private
  val maxU = 0.65F
  def get(k:String) = mData.getOrElse(k, maxU)
  
  def isIn(n:String) = {
    mData.contains(n)
  }

  def insert(k:String,v:Float) {
//    if (mData.contains(k)){
//      if (mData(k) < v)
//        mData(k) = v
//    }
//    else {
//      mData(k) = v
//    }
    mData(k) = v
  }

  def put(k: String, v: Float) {
    if (v < maxU) {
      if (mData.contains(k)) {
        if (mData(k) < v)
          mData(k) = v
      } else {
        mData(k) = v
      }
    } else {

    }

  }
    
  def remove(k:String) {
    mData.remove(k)
  }
  
  def clear = {
    mData.clear
  }
  
}

object UpperDict {
  
  def init(location:String) = {
    val vu = new UpperDict
    val source = Source.fromFile(location,"UTF-8")
    val lineIterator = source.getLines
    //val ms = new ArrayBuffer[(String,Float)]
    for (line <- lineIterator) {
      val Array(v,w) = line.trim.split("\t")
      //ms += ((v,w.toFloat))
      vu.insert(v, w.toFloat)
    }
    source.close
    //ms.toMap
    vu
  }
  
  
}